package com.gexiaobao.pigeon.ui.fragment.mine.tools

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.annotation.Nullable
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.util.WxShareUtils
import com.gexiaobao.pigeon.databinding.FragmentVideoPlayerBinding
import com.gexiaobao.pigeon.viewmodel.MineToolsViwModel
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat


/**
 * @Author      : hxw
 * @Date        : 2023/6/1 9:40
 * @Describe    : 视频播放
 */
class FragmentVideoPlayer : BaseFragment<MineToolsViwModel, FragmentVideoPlayerBinding>() {

    private var videoUrl = ""
    private var title = ""
    private var mImageUrl = ""
    private var mDesc = ""

    private var orientationUtils: OrientationUtils? = null

    override fun initView(savedInstanceState: Bundle?) {
        videoUrl = arguments?.getString("videoUrl").toString()
        title = arguments?.getString("title").toString()
        mImageUrl = arguments?.getString("imageUrl").toString()
        mDesc = arguments?.getString("desc").toString()

        mDatabind.includeBar.toolbar.initClose(title) {
            nav().navigateUp()
        }
        mDatabind.includeBar.tvRightMenu.text = "分享"
        mDatabind.includeBar.tvRightMenu.background = ContextCompat.getDrawable(requireContext(), R.drawable.round_tv_red_stroke20_bg)
    }

    override fun initData() {
        super.initData()
        mDatabind.videoPlayer.setUp(videoUrl, true, title)
        initVideoPlayer()
    }

    private fun initVideoPlayer() {
        //增加封面
        val imageView = ImageView(context)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        imageView.setImageResource(R.mipmap.app_icon_)
        mDatabind.videoPlayer.thumbImageView = imageView
        //增加title
        mDatabind.videoPlayer.titleTextView.visibility = View.VISIBLE
        //设置返回键
        mDatabind.videoPlayer.backButton.visibility = View.VISIBLE
        //设置旋转
        orientationUtils = OrientationUtils(activity, mDatabind.videoPlayer)
        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        mDatabind.videoPlayer.fullscreenButton.setOnClickListener { // ------- ！！！如果不需要旋转屏幕，可以不调用！！！-------
            // 不需要屏幕旋转，还需要设置 setNeedOrientationUtils(false)
            //orientationUtils.resolveByClick();
            nav().navigateUp()
        }
        //是否可以滑动调整
        mDatabind.videoPlayer.setIsTouchWiget(true)
        //设置返回按键功能
        mDatabind.videoPlayer.backButton.setOnClickListener { }
        ///不需要屏幕旋转
        mDatabind.videoPlayer.isNeedOrientationUtils = false

        mDatabind.videoPlayer.startPlayLogic()
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.tvRightMenu) {
            when (it) {
                mDatabind.includeBar.tvRightMenu -> {
                    shareVideo()
                }
            }
        }
    }

    private fun shareVideo() {
        Thread {
            Glide.with(this).asBitmap().load(mImageUrl).into(object : SimpleTarget<Bitmap?>() {
                override fun onLoadFailed(@Nullable errorDrawable: Drawable?) {
                    super.onLoadFailed(errorDrawable)
                    WxShareUtils.shareVideo(context, videoUrl, title, mDesc, null)
                }

                override fun onResourceReady(thumbBmp: Bitmap, transition: Transition<in Bitmap?>?) {
                    WxShareUtils.shareVideo(context, videoUrl, title, mDesc, thumbBmp)
                }
            })
        }.start()
    }

    override fun onPause() {
        super.onPause()
        mDatabind.videoPlayer.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        mDatabind.videoPlayer.onVideoResume()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        GSYVideoManager.releaseAllVideos()
        if (orientationUtils != null) orientationUtils!!.releaseListener()
    }
}